/**
 * @class   wosh::PluginModule
 * @brief   
 *          
 *          
 *          
 ****************************************************************************
 * @version $Id: PluginModule.h 2488 2010-05-21 01:48:27Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

/*
g++ -fPIC -shared BundlePlugin.cpp -o myclass.so
g++ main.cpp -ldl -o sample
*/

#ifndef __WOSH_Plugins_PluginModule_H__
 #define __WOSH_Plugins_PluginModule_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/LogContext.h>
 #include <core/Containers.h>
 #include <core/FileSystem.h>
 #include <string>


namespace wosh {
 namespace plugins {

// represent a dynamic module. (holds the lib info and status)

class PluginModule : public Object  {

	public:
		/// types of the data
		enum MODULE_STATE {
			STATE_UNKNOWN = 0,
			STATE_IDLE = 1,
			STATE_OPENED = 2,
			STATE_INITIALIZED = 4,
			STATE_PARSED = 16,
			STATE_ERROR = 64
		};

		/// types of the data
		enum MODULE_TYPE {
			TYPE_UNKNOWN = 0,
			TYPE_BUNDLE = 1,
			TYPE_NETWORK = 2,
			TYPE_DATABASE = 8,
			TYPE_BUILDING = 16,
			TYPE_AUTOMATION = 32
		};

	public:
		PluginModule();

		virtual ~PluginModule();


	public:
		virtual WRESULT open( const std::string& library_file );
		virtual WRESULT open();

		virtual WRESULT close();


	public:
		int getModuleState() const								{ return this->state; }
		int getModuleType() const								{ return this->library_type; }

		bool isModuleOpened() const								{ return (this->state & (int)PluginModule::STATE_OPENED ); }
		bool isModuleParsed() const								{ return (this->state & (int)PluginModule::STATE_PARSED ); }


		const ListT<std::string>& getObjectTypes() const		{ return this->objects_type; }
		ListT<std::string>& getObjectTypes()					{ return this->objects_type; }

		const MapT<std::string, Object*>& getObjects() const	{ return this->objects; }
		MapT<std::string, Object*>& getObjects()				{ return this->objects; }


	public:
		virtual WRESULT initFW();

		virtual IReflection* alloc( const std::string& object_type );

		virtual WRESULT dealloc();
		virtual WRESULT dealloc( const std::string& object_uuid );

		virtual WRESULT loadInfo( bool load_if_required = true );

		virtual bool supportsObjectType( const std::string& object_type ) const;


	public:
		WRESULT setLibraryFile( const std::string& library_file );


	protected:	// module information
		FilePath library_file;
		int state;
		void* handle;

	protected:	// plugin info (loaded from lib)
		std::string library_name;
		std::string library_uuid;
		int library_type;

		std::string library_author;
		double version_mayor;
		double version_minor;

		ListT<std::string> objects_type;
		MapT<std::string, Object*> objects; ///< UID -> pointer

		wosh::LogContext Log;


}; // class def

 }; // namespace plugins
}; // namespace wosh

#endif //__WOSH_Plugins_PluginModule_H__
